package com.util.tool;

import java.io.File;
import java.util.List;

import android.os.Handler;
import android.util.Log;

import com.entity.DingDan;
import com.entity.DrivdeInfo;
import com.entity.Login;
import com.entity.MessageInfor;
import com.entity.Msg;
import com.entity.UserCarInfor;
import com.entity.UserInfor;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;



public class ApiConnHelp {
	public static final String baseUrl = "http://www.zhaodabus.com:8080/ShuttleBus/";
	private static Gson gson;

	static {
		gson = new Gson();
	}

	/**
	 * 1、注册(客户端) 要的是MessageInfor里面的flag;
	 * 
	 * @param identityStatus
	 * @param phone
	 * @param passWord
	 * @param businessImg
	 * @param identityImg
	 * @param companyStatus
	 * @param companyName
	 * @param businessNo
	 * @param attenName
	 * @param attenPhone
	 * @return flag: 0手机号存在 1成功 -1异常
	 */
	public static MessageInfor register(String identityStatus, String phone,
			String passWord, String businessImg, String identityImg,
			String companyStatus, String companyName, String businessNo,
			String attenName, String attenPhone) {
		String url = baseUrl
				+ "CustomerAction!customerRegister.action?identityStatus="
				+ identityStatus
				+ "&phone="
				+ phone
				+ "&passWord="
				+ passWord
				+ "&businessImg="
				+ businessImg
				+ "&identityImg="
				+ identityImg
				+ "&companyStatus="
				+ companyStatus
				+ "&companyName="
				+ companyName
				+ "&businessNo="
				+ businessNo
				+ "&attenName="
				+ attenName + "&attenPhone=" + attenPhone;
		String json = HttpHelper.loadStringFromUrl(url);

		return gson.fromJson(json, MessageInfor.class);
	}


	/**
	 * 2、登录(客户端)
	 * 
	 * @param phone
	 * @param passWord
	 * @return flag: 0手机号与密码不一致 1没有审核 2是否强制登录 3登录成功 -1异常id: 用户id:
	 *         (是否强制登录和登录成功返回)tagId: 用于发送消息绑定tag: 和 用于检测是否在线
	 *         (登录成功返回)identityStatus :身份标示0 个人 1单位headImg:vipStatus :是否是vip 0不是
	 *         1是
	 */
	public static Login login(String phone, String passWord) {
		String url = baseUrl + "CustomerAction!customerLogin.action?phone="
				+ phone + "&passWord=" + passWord;
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, Login.class);
	}

	
	/**
	 * 3、强制登录(客户端,服务端)
	 * @param id 登录人
	 * @param tag 0客户端 1服务端
	 * @return flag:	0失败 1登录成功 -1异常
	 */
	public static Login login_force(String id, String tag) {
		String url = baseUrl + "CustomerAction!forceLogin.action?id="
				+ id + "&tag=" + tag;
		Log.i("Text", "===>url"+url);
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, Login.class);
	}
	
	/**
	 * 4、忘记密码(客户端,服务端)
	 * 
	 * @param phone
	 * @param passWord
	 * @param tag 0客户端 1服务端
	 * @return flag: 0失败 1成功 -1异常
	 */
	public static MessageInfor forgetPassword(String phone, String passWord,
			String tag) {
		String url = baseUrl + "CustomerAction!forgetPassWord.action?phone="
				+ phone + "&passWord=" + passWord + "&tag=" + tag;
		String json = HttpHelper.loadStringFromUrl(url);

		return gson.fromJson(json, MessageInfor.class);
	}

	/**
	 * 5、短信验证(客户端,服务端)
	 * 
	 * @param phone
	 * @return flag:1成功 -1;异常 codes: 成功后返回
	 */
	public static MessageInfor messageTest(String phone) {
		String url = baseUrl + "CustomerAction!sendMsg.action?phone="
				+ phone;
		String json = HttpHelper.loadStringFromUrl(url);

		return gson.fromJson(json, MessageInfor.class);
	}

	/**
	 * 6、个人信息获得(客户端) /CustomerAction!customerList.action?id=&tagId=
	 * 
	 * @param id
	 *            : 用户id
	 * @param tagId
	 *            : 在线库id 绑定tag的值 登录时存储的tagId
	 * @return 用户信息
	 */
	public static UserInfor userInfor(String id, String tagId) {
		String url = baseUrl
 + "CustomerAction!customerList.action?id=" + id
				+ "&tagId=" + tagId;
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, UserInfor.class);
	}

	/**
	 * 7、编辑资料(客户端)
	 * 
	 * @param userInfor
	 *            id: 用户id （必填） tagId 在线库id 绑定tag的值 登录时存储的tagId （必填） nickName 昵称
	 *            sex 性别 attenName 联系人姓名 attenPhone 联系人手机
	 * @return flag -2 其他设备登录 退出到登录页 0失败 1成功
	 */
	public static MessageInfor updataUserInfor(UserInfor userInfor) {
		String url = baseUrl
 + "CustomerAction!customerEdit.action?id="
				+ userInfor.getId() + "&tagId=" + userInfor.getTagId()
				+ "&attenNae=" + userInfor.getAttenName() + "&attenPhone="
				+ userInfor.getAttenPhone() + "&nickName="
				+ userInfor.getNickName() + "&sex=" + userInfor.getSex();
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, MessageInfor.class);
	}



	/**
	 * 8、修改密码(客户端)
	 * 
	 * @param id
	 *            用户id
	 * @param tagId
	 *            在线库id
	 * @param oldPassWord
	 *            原密码
	 * @param newPassWord
	 *            新密码
	 * @return flag -2 其他设备登录 退出到登录页 0失败 1原密码不对2成功
	 */
	public static MessageInfor updataPassWord(String id, String tagId, String oldPassWord, String newPassWord) {
		String url = baseUrl + "CustomerAction!customerPassWord.action?id="
				+ id + "&tagId=" + tagId
				+ "&passWord=" + oldPassWord + "&passWord1=" + newPassWord;
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, MessageInfor.class);
	}


	/**
	 * 9、上传经纬度
	 * 
	 * @param id
	 *            用户id
	 * @param longitude
	 *            经度
	 * @param latitude
	 *            纬度
	 * @param type
	 *            0客户端1服务端 按坐标变化1km后上传
	 * @return flag 1成功 -1异常
	 */
	public static MessageInfor updataCoordinate(String id, String longitude, String latitude, String type) {
		String url = baseUrl + "SalesmanAction!itItudeAdd.action?id=" + id
				+ "&longitude=" + longitude
				+ "&latitude=" + latitude + "&type=" + type;
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, MessageInfor.class);
	}

	/**
	 * 10、首页(客户端)
	 * 
	 * @param longitude
	 *            经度
	 * @param latitude
	 *            纬度
	 * @return imgUrl 图片地址 linkUrl 图片链接 (图片点击后的链接)
	 */
	public static List<Msg> homePager(String longitude, String latitude) {
		String url = baseUrl + "CustomerAction!firstPage.action?longitude="
				+ longitude + "&latitude="
				+ latitude;
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, DingDan.class).getmsg();
	}

	/**
	 * 20、修改头像(客户端，服务端)
	 * 
	 * @param handler
	 *            what 1成功 0失败 -2其他设备登录 -1网络异常
	 * @param id
	 *            用户id
	 * @param tagId
	 *            在线库id
	 * @param type
	 *            0客户端1服务端
	 * @param headImg
	 *            头像
	 */
	public static void updataHeadImage(final Handler handler, String id, String tagId, String type, File headImg) {
		String url = baseUrl + "CustomerAction!editHeadImg.action?id=" + id
				+ "&tagId=" + tagId + "&type="
				+ type;
		HttpUtils utils = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addBodyParameter("headImg", headImg);
		utils.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				handler.sendEmptyMessage(-1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				String flag = gson.fromJson(arg0.result, MessageInfor.class).getFlag()+"";
				handler.sendEmptyMessage(Integer.parseInt(flag));
			}
		});
	}

	/**
	 * 21、获得我的余额和等级
	 * 
	 * @param id
	 *            用户id
	 * @param type
	 *            0客户端 1服务端
	 * @return money 我的金钱 level 我的等级
	 * 
	 */
	public static UserInfor getLevel(String id, String type) {
		String url = baseUrl + "OrdersAction!getMyMoneyAndLevel.action?id="
				+ id + "&type=" + type;
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, UserInfor.class);
	}
	/**
	 * 22、反馈(客户端，服务端)
	 * 
	 * @param id
	 *            用户id
	 * @param tagId
	 *            在线库id
	 * @param type
	 *            0客户端1服务端
	 * @param title
	 *            标题
	 * @param content
	 *            内容
	 * @return flag 1成功 0失败 -2其他设备登录
	 */
	public static MessageInfor feedback(String id, String tagId, String type, String title, String content) {
		String url = baseUrl + "FeedBackAction!feedBackList.action?id=" + id
				+ "&tagId=" + tagId + "&type="
				+ type + "&title=" + title + "&content=" + content;
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, MessageInfor.class);
	}

	/**
	 * 23、退出登录
	 * 
	 * @param id
	 *            用户id
	 * @param type
	 *            0客户端1服务端
	 * @return flag 1成功 0失败 -1异常
	 */
	public static MessageInfor outLogin(String id, String type) {
		String url = baseUrl + "CustomerAction!exitLogin.action?id=&tag=" + id
				+ "&tag=" + type;
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, MessageInfor.class);
	}

	/*
	 * public static MessageInfor orderByVip(String id, String tagId) { String
	 * url = baseUrl + "CustomerAction!exitLogin.action?id=&tag=" + id + "&tag="
	 * + tagId; String json = HttpHelper.loadStringFromUrl(url); return
	 * gson.fromJson(json, MessageInfor.class); }
	 */

	/**
	 * 26、一键用车下单
	 * 
	 * @param car
	 * @return flag 1成功 -1异常 -2其他用户登录
	 */
	public static MessageInfor oneKeyCar(UserCarInfor car) {
		return orders("oneKeyCar", car);
	}

	/**
	 * 27、接送机场、车站下单
	 * 
	 * @param car
	 *            1成功 -1异常 -2其他用户登录
	 * @return
	 */
	public static MessageInfor planeOrders(UserCarInfor car) {
		String url = baseUrl + "OrdersAction!planeOrders.action?id="
				+ car.getId() + "&tagId="
				+ car.getTagId() + "&tripStatus=" + car.getTripStatus() + "&tripWay=" + car.getTripWay()
				+ "&tripPeople=" + car.getTripPeople() + "&triptime=" + car.getTriptime() + "&startAdress="
				+ car.getStartAdress() + "&slongitude=" + car.getSlongitude() + "&slatitude=" + car.getSlatitude()
				+ "&endAdress=" + car.getEndAdress() + "&elonitude=" + car.getElongitude() + "&elatitude="
				+ car.getElatitude() + "&tripPrice=" + car.getTripPrice() + "&carkId=" + car.getCarkId()
				+ "&tripMileage="
				+ car.getTripmileage(); /*
										 * + "&tripNumber=" +
										 * car.getTripNumber() + "&tripDay=" +
										 * car.getTripDay() + "&tripMileage=" +
										 * car.getTripmileage() + "&tripCar=" +
										 * car.getTripCar() + "&tripCarPre=" +
										 * car.getTripPrice();
										 */
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, MessageInfor.class);
	}

	/**
	 * 28、日租一日游下单
	 * 
	 * @param car
	 * @return flag 1成功 -1异常 -2其他用户登录
	 */
	public static MessageInfor oneDayOrders(UserCarInfor car) {
		return orders("oneDayOrders", car);
	}

	/**
	 * 29、各种展会、会议下单
	 * 
	 * @param car
	 * @return flag 1成功 -1异常 -2其他用户登录
	 */
	public static MessageInfor meetOrders(UserCarInfor car) {
		return orders("meetOrders", car);
	}

	/**
	 * 30、各种班车、校车下单
	 * 
	 * @param car
	 * @return
	 */
	public static MessageInfor busOrders(UserCarInfor car) {
		return orders("busOrders", car);
	}

	/**
	 * 36、选择司机(按司机)
	 * 
	 * @param driverId
	 * @return
	 */
	public static DrivdeInfo choseDriver(String driverId) {
		String url = baseUrl + "SalesmanAction!ordersSiji.action?id="
				+ driverId;
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, DrivdeInfo.class);
	}

	/**
	 * 37、政府采购检索
	 * 
	 * @param DingDanId
	 * @return
	 */
	public static DrivdeInfo governmentCar(String DingDanId) {
		String url = baseUrl + "SalesmanAction!governmentCar.action?id="
				+ DingDanId;
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, DrivdeInfo.class);
	}

	/**
	 * 38、车型选择检索
	 * 
	 * @param driverId
	 * @return
	 */
	public static DrivdeInfo choseCar(String DingDanId) {
		String url = baseUrl + "SalesmanAction!ordersSiji.action?id="
				+ DingDanId;
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, DrivdeInfo.class);
	}

	/**
	 * 39、提交订单合并价格
	 * @param DingDanId
	 * @return
	 */
	public static MessageInfor ordersMerge(String DingDanId, String price) {
		String url = baseUrl + "OrdersAction!ordersMerge.action?id="
				+ DingDanId + "&price=" + price;
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, MessageInfor.class);
	}

	
	private static MessageInfor orders(String orders, UserCarInfor car) {
		String url = baseUrl + "OrdersAction!" + orders + ".action?id="
				+ car.getId() + "&tagId="
				+ car.getTagId() + "&tripStatus=" + car.getTripStatus() + "&tripWay=" + car.getTripWay()
				+ "&tripPeople=" + car.getTripPeople() + "&triptime=" + car.getTriptime() + "&startAdress="
				+ car.getStartAdress() + "&slongitude=" + car.getSlongitude() + "&slatitude=" + car.getSlatitude()
				+ "&endAdress=" + car.getEndAdress() + "&elonitude=" + car.getElongitude() + "&elatitude="
				+ car.getElatitude() + "&tripPrice=" + car.getTripPrice() + "&carkId=" + car.getCarkId()
				+ "&tripNumber=" + car.getTripNumber() + "&tripDay=" + car.getTripDay() + "&tripMileage="
				+ car.getTripmileage() + "&tripCar=" + car.getTripCar() + "&tripCarPre=" + car.getTripPrice();
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, MessageInfor.class);
	}
	/**
	 * 40、司机的抢单数量
	 * 
	 * @param id
	 * @return count: 数量
	 */
	public static MessageInfor driverRobCount(String id) {
		String url = baseUrl + "SalesmanAction!myOrdersCount.action?id="
				+ id;
		String json = HttpHelper.loadStringFromUrl(url);
		return gson.fromJson(json, MessageInfor.class);
	}
	
	

}
